package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Evaluate;
import com.itheima.service.AssessService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 评估建议
 */

@RestController
@RequestMapping("/assess")
public class AssessController {
    @Reference
    private AssessService assessService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return assessService.findPage(queryPageBean);
    }

    //新增
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    public Result add(@RequestBody Evaluate evaluate){
        try{
            assessService.add(evaluate);//发送请求
            return new Result(true, MessageConstant.ADD_ASSESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ASSESS_FAIL);
        }
    }

    //根据id删除检查项
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result delete(Integer id){
        try{
            assessService.delete(id);//发送请求
            return new Result(true, MessageConstant.DELETE_ASSESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ASSESS_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result edit(@RequestBody Evaluate evaluate){
        try{
            assessService.edit(evaluate);//发送请求
            return new Result(true, MessageConstant.EDIT_ASSESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ASSESS_FAIL);
        }
    }


    //根据id查询检查项
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Evaluate evaluate = assessService.findById(id);//发送请求
            return new Result(true, MessageConstant.QUERY_ASSESS_SUCCESS,evaluate);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ASSESS_FAIL);
        }
    }


}

